﻿using System.Collections.Generic;



namespace Asion.Dapper
{
    public class PageReponse<TEntity>
    {


        public PageReponse(int currentPage, int rowsPerPage, int total, IEnumerable<TEntity> data)
        {
            this.PageNumber = currentPage;
            this.RowsPerPage = rowsPerPage;
            this.Total = total;
            if (data == null)
            {
                data = new List<TEntity>();
            }
            this.Rows = data;

        }


        /// <summary>
        /// query all-rows count
        /// </summary>
        public int Total { get; set; }



        /// <summary>
        /// page size
        /// </summary>
        public int RowsPerPage { get; set; }


        /// <summary>
        /// current page number
        /// </summary>
        public int PageNumber { get; set; }



        /// <summary>
        /// total page count
        /// </summary>
        public int PageTotal
        {
            get { return GetPageTotal(); }
        }

        /// <summary>
        /// calculate page total
        /// </summary>
        /// <returns></returns>
        public int GetPageTotal()
        {
            var total = this.Total;
            var pagesize = this.RowsPerPage;
            if (total <= 0)
            {
                return 0;
            }
            if (pagesize <= 0)
            {
                return 0;
            }
            if (total < pagesize)
            {
                return 1;
            }
            var size = total / pagesize;
            var overSize = total % pagesize;
            var result = size;
            if (overSize > 0)
            {
                result += 1;
            }
            return result;
        }




        /// <summary>
        /// return data
        /// </summary>
        public IEnumerable<TEntity> Rows { get; set; }

    }
}
